package com.snda.lib.util;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.provider.MediaStore;

public class IntentHelper {

	/**
	 * Indicates whether the specified action can be used as an intent. This
	 * method queries the package manager for installed packages that can
	 * respond to an intent with the specified action. If no suitable package is
	 * found, this method returns false.
	 * 
	 * @strFileName context
	 *            The application's environment.
	 * @strFileName action
	 *            The Intent action to check for availability.
	 * 
	 * @return True if an Intent with the specified action can be sent and
	 *         responded to, false otherwise.
	 */
	public static boolean isIntentAvailable(Context context, String action) {

		final PackageManager packageManager = context.getPackageManager();
		final Intent intent = new Intent(action);
		List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
				PackageManager.MATCH_DEFAULT_ONLY);
		return list.size() > 0;
	}

	/**
	 * Indicates whether the specified action can be used as an intent. This
	 * method queries the package manager for installed packages that can
	 * respond to an intent with the specified action. If no suitable package is
	 * found, this method returns false.
	 * 
	 * @strFileName context
	 *            The application's environment.
	 * @strFileName action
	 *            The Intent action to check for availability.
	 * 
	 * @return True if an Intent with the specified action can be sent and
	 *         responded to, false otherwise.
	 */
	public static boolean isIntentAvailable(Context context, Intent intent) {
		if (context == null || intent == null)
			return false;

		final PackageManager packageManager = context.getPackageManager();
		List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
				PackageManager.MATCH_DEFAULT_ONLY);
		return list.size() > 0;
	}

	/**
	 * android获取一个用于打开照相机的intent
	 * 
	 * @return
	 */
	public static Intent getCameraIntent() {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		return intent;
	}

	/**
	 * android获取一个用于打开Apk文件的intent
	 * 
	 * @strFileName strApkFile
	 * @return
	 */
	public static Intent getApkFileIntent(String strApkFile) {

		File apkFile = new File(strApkFile);

		Uri uri = Uri.fromFile(apkFile);
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(uri, "application/vnd.android.package-archive");

		return intent;
	}

	/**
	 * android获取一个用于打开HTML文件的intent
	 * 
	 * @strFileName strFileName
	 * @return
	 */
	public static Intent getHtmlFileIntent(String strFileName) {

		Uri uri = Uri.parse(strFileName).buildUpon()
				.encodedAuthority("com.android.htmlfileprovider")
				.scheme("content").encodedPath(strFileName).build();

		Intent intent = new Intent("android.intent.action.VIEW");
		intent.setDataAndType(uri, "text/html");
		return intent;
	}

	/**
	 * android获取一个用于打开图片文件的intent
	 * 
	 * @strFileName strFileName
	 * @return
	 */
	public static Intent getImageFileIntent(String strFileName) {
		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		Uri uri = Uri.fromFile(new File(strFileName));
		intent.setDataAndType(uri, "image/*");
		return intent;
	}

	/**
	 * android获取一个用于打开图片文件的intent
	 * 
	 * @return
	 */
	public static Intent getImageFileIntent() {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("image/*");
		return intent;
	}

	/**
	 * android获取一个用于打开PDF文件的intent
	 * 
	 * @strFileName strFileName
	 * @return
	 */
	public static Intent getPdfFileIntent(String strFileName) {

		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Uri uri = Uri.fromFile(new File(strFileName));
		intent.setDataAndType(uri, "application/pdf");
		return intent;
	}

	/**
	 * android获取一个用于打开文本文件的intent
	 * 
	 * @strFileName strFileName
	 * @strFileName paramBoolean
	 * @return
	 */
	public static Intent getTextFileIntent(String strFileName, boolean paramBoolean) {

		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		if (paramBoolean) {
			Uri uri1 = Uri.parse(strFileName);
			intent.setDataAndType(uri1, "text/plain");
		} else {
			Uri uri2 = Uri.fromFile(new File(strFileName));
			intent.setDataAndType(uri2, "text/plain");
		}

		return intent;
	}

	/**
	 * android获取一个用于打开音频文件的intent
	 * 
	 * @strFileName strFileName
	 * @return
	 */
	public static Intent getAudioFileIntent(String strFileName) {

		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.putExtra("oneshot", 0);
		intent.putExtra("configchange", 0);

		Uri uri = Uri.fromFile(new File(strFileName));
		intent.setDataAndType(uri, "audio/*");
		return intent;
	}

	/**
	 * android获取一个用于打开视频文件的intent
	 * 
	 * @strFileName strFileName
	 * @return
	 */
	public static Intent getVideoFileIntent(String strFileName) {

		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.putExtra("oneshot", 0);
		intent.putExtra("configchange", 0);

		Uri uri = Uri.fromFile(new File(strFileName));
		intent.setDataAndType(uri, "video/*");
		return intent;
	}

	/**
	 * android获取一个用于打开CHM文件的intent
	 * 
	 * @strFileName strFileName
	 * @return
	 */
	public static Intent getChmFileIntent(String strFileName) {

		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		Uri uri = Uri.fromFile(new File(strFileName));
		intent.setDataAndType(uri, "application/x-chm");
		return intent;
	}

	/**
	 * android获取一个用于打开Word文件的inten
	 * 
	 * @strFileName strFileName
	 * @return
	 */
	public static Intent getDocFileIntent(String strFileName) {

		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		Uri uri = Uri.fromFile(new File(strFileName));
		intent.setDataAndType(uri, "application/msword");
		return intent;
	}

	/**
	 * android获取一个用于打开Excel文件的intent
	 * 
	 * @strFileName strFileName
	 * @return
	 */
	public static Intent getXlsFileIntent(String strFileName) {

		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		Uri uri = Uri.fromFile(new File(strFileName));
		intent.setDataAndType(uri, "application/vnd.ms-excel");
		return intent;
	}

	/**
	 * android获取一个用于打开PPT文件的intent
	 * 
	 * @strFileName strFileName
	 * @return
	 */
	public static Intent getPptFileIntent(String strFileName) {

		Intent intent = new Intent("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		Uri uri = Uri.fromFile(new File(strFileName));
		intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
		return intent;
	}

	public static Intent getOpenFileIntent(String strFileName) {
		int nIndex = strFileName.lastIndexOf('.');
		if (nIndex != -1) {
			String strExtName = strFileName.substring(nIndex + 1,
					strFileName.length());

			// 检查是否是doc文件
			if (strExtName.equalsIgnoreCase("doc") == true) {
				return getDocFileIntent(strFileName);
			}
			// 检查是否是ppt文件
			else if (strExtName.equalsIgnoreCase("ppt") == true) {
				return getPptFileIntent(strFileName);
			}
			// 检查是否是Xls件
			else if (strExtName.equalsIgnoreCase("xls") == true) {
				return getXlsFileIntent(strFileName);
			}
			// 检查是否是pdf文件
			else if (strExtName.equalsIgnoreCase("pdf") == true) {
				return getPdfFileIntent(strFileName);
			}
			// 检查是否是mp3文件
			else if (strExtName.equalsIgnoreCase("mp3") == true) {
				return getAudioFileIntent(strFileName);
			}
			// 检查是否是wav文件
			else if (strExtName.equalsIgnoreCase("wav") == true) {
				return getVideoFileIntent(strFileName);
			}
		}

		return null;
	}

}
